package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    SpuService spuService;
    @Autowired
    SpuDetailService spuDetailService;
    @Autowired
    SkuService skuService;
    @Autowired
    SpecParamService specParamService;

    /**
     *  分页查询
     * @param page
     * @param rows
     * @param bid
     * @param cid
     * @param id
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "10")Integer rows,
            @RequestParam(value = "brandId",required = false)Integer bid,
            @RequestParam(value = "categoryId",required = false)Integer cid,
            @RequestParam(value = "id",required = false)Integer id,
            @RequestParam(value = "saleable",required = false)Boolean saleable

    ) {
        return ResponseEntity.ok(spuService.querySpuByPage(page,rows,bid,cid,id,saleable));
    }

    /**
     *  新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu") //接口文档地址有误，没有加载到方法
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) { //请求参数过多，使用@requestBody
        System.out.println("****** spuDTO ******= " + spuDTO);
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(201).build();
    }

    /**
     *  更新商品
     * @param spuDTO
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(204).build();
    }

    /**
     * 根据sku id 批量查询sku的集合
     * @param ids
     * @return
     */
    @RequestMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids")List<Long> ids) {
        List<Sku> listSku = skuService.listByIds(ids);
        return ResponseEntity.ok(SkuDTO.convertEntityList(listSku));
    }

    /**
     * 根据spuid查询sku的集合
     * @param SpuId
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id")Long SpuId) {
        List<SkuDTO> skuDTOList = skuService.querySkuBySpuId(SpuId);
        return ResponseEntity.ok(skuDTOList);
    }

    /**
     * 根据商品id(spuid) (自己用skuid也能查询)查询商品详情
     * @param Skuid
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id")Long Skuid){
        SpuDetailDTO spuDetailDTO = skuService.querySpuDetailById(Skuid);
        return ResponseEntity.ok(spuDetailDTO);
    }

    /**
     * 根据spuid 查询spu的所有规格参数值,主表查询Spec_param
     * 未做查询结果的优化
     * @param spuId
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    // 规格参数值就是spuDetail的特殊字段specification
    public ResponseEntity<List<SpecParamDTO>> querySpecParamDTOBySpuId(@RequestParam("id")Long spuId,@RequestParam(value = "searching",required = false)Boolean searching){
        List<SpecParamDTO> specParamDTOList = specParamService.querySpecValues(spuId,searching);
        return ResponseEntity.ok(specParamDTOList);
    }


    /**
     *  修改商品上下架
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSkuById(@RequestParam("id")Long id,@RequestParam("saleable")Boolean saleable) {
        spuService.updateSaleable(id,saleable);
        return ResponseEntity.status(204).build();
    }

    /**
     * 根据spuId商品id 查询SPU,SKU,SKUDetail
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> querySpuSpuDSkuById(@PathVariable("id")Long id){
       SpuDTO spuDTO = spuService.querySpuSpuDSkuById(id);
        return ResponseEntity.ok(spuDTO);
    }

    /**
     * 根据商品id(spuid)查询商品
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuDTOById(@PathVariable("id")Long id){
        Spu spu = spuService.getById(id);
        return ResponseEntity.ok(new SpuDTO(spu));
    }

    /**
     * 批量减库存 TODO 接口API少参数？？
     * @param skuID
     * @param num
     * @return
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> updateSkuByskuId(@RequestParam("id")Long skuID,@RequestParam("cellNum") Integer num){
        skuService.updateSkuByskuId(skuID,num);
        return ResponseEntity.status(204).build();
    }


}
